//go:generate goagen bootstrap -d git.oschina.net/antlinker/antevent/server/design

package main

import (
	"log"
	"os"
	"os/signal"
	"time"

	"github.com/antlinker/sdk/asapi"

	"github.com/coreos/etcd/clientv3"
	"gopkg.in/urfave/cli.v2"

	"git.oschina.net/antlinker/antevent/server"
	"git.oschina.net/antlinker/antmqtt/debug"
)

func main() {

	initArgs(os.Args)

}

func createStart() []cli.Flag {
	//jobid string, gorunCnt int, loadSpaceTime time.Duration, maxerrcnt
	fs := []cli.Flag{
		&cli.StringFlag{
			Name:    "listen",
			Aliases: []string{"l"},
			Value:   ":8080",
			Usage:   "监听ip:port",
		},
		&cli.StringFlag{
			Name:    "servername",
			Aliases: []string{"sn"},
			Value:   "tmp",
			Usage:   "服务名称",
		},
		&cli.StringFlag{
			Name:    "mgourl",
			Aliases: []string{"ml"},
			Value:   "localhost:27017",
			Usage:   "mongodb数据库连接",
		},
		&cli.StringFlag{
			Name:    "mgodbname",
			Aliases: []string{"mn"},
			Value:   "antevent",
			Usage:   "mongodb数据库名",
		},
		&cli.StringSliceFlag{
			Name:    "etcdendpoint",
			Aliases: []string{"eep"},
			Value:   cli.NewStringSlice("localhost:2379"),
			Usage:   "etcd数据库连接",
		},
		&cli.IntFlag{
			Name:    "etcdtimeout",
			Aliases: []string{"eto"},
			Value:   10,
			Usage:   "etcd数据库连接超时时间",
		},
		&cli.IntFlag{
			Name:    "hooktimeout",
			Aliases: []string{"hto"},
			Value:   10,
			Usage:   "回调请求超时时间",
		},
		&cli.IntFlag{
			Name:    "errtrycnt",
			Aliases: []string{"etc"},
			Value:   3,
			Usage:   "回调请求错误重试次数",
		},
		&cli.StringFlag{
			Name:    "authurl",
			Aliases: []string{"au"},
			Value:   "http://192.168.1.202:8090",
			Usage:   "认证中心连接",
		},
		&cli.StringFlag{
			Name:    "sercret",
			Aliases: []string{"s"},
			Value:   "",
			Usage:   "认证中心连接",
		},
		&cli.StringFlag{
			Name:    "clientid",
			Aliases: []string{"cid"},
			Value:   "",
			Usage:   "认证中心分配的客户端id",
		},
		&cli.StringFlag{
			Name:    "serverid",
			Aliases: []string{"sid"},
			Value:   "",
			Usage:   "服务标识",
		},
		&cli.StringFlag{
			Name:    "pprof",
			Aliases: []string{"p"},
			Value:   "",
			Usage:   "启动pprof分析程序,默认不启动，可以设置:9090,指定一个监听ip端口通过http访问",
		},
		&cli.StringFlag{
			Name:    "jwtSignedKey",
			Aliases: []string{"jsk"},
			Value:   "jwtSignedKey",
			Usage:   "认证ｊｗｔ令牌",
		},
		&cli.StringFlag{
			Name:    "userIdentify",
			Aliases: []string{"uid"},
			Value:   "ANT",
			Usage:   "用户认证系统标识",
		},
	}
	return fs
}

func pprof(c *cli.Context) {
	pprofaddr := c.String("pprof")
	if pprofaddr != "" {
		debug.StartHTTPPprof(pprofaddr)
	}
}
func initArgs(args []string) {
	app := &cli.App{
		Authors: []*cli.Author{
			{Name: "@antlinker.com"},
		},
		Name:    "事件引擎",
		Version: "1.0",
		Usage:   "事件调度引擎,可以将事件发布给订阅者",

		Flags: createStart(),
		Action: func(c *cli.Context) error {
			pprof(c)
			var cfg clientv3.Config
			cfg.Endpoints = c.StringSlice("etcdendpoint")
			cfg.DialTimeout = time.Duration(c.Int("etcdtimeout")) * time.Second
			opt := server.Option{
				AuthCfg: asapi.Config{
					ASURL:           c.String("authurl"),
					ClientID:        c.String("clientid"),
					ClientSecret:    c.String("sercret"),
					ServiceIdentify: c.String("serverid"),
				},
				EtcdCfg:      cfg,
				Listen:       c.String("listen"),
				ServerName:   c.String("servername"),
				TryCnt:       c.Int("errtrycnt"),
				TimeOut:      c.Int("hooktimeout"),
				JWTSignedKey: c.String("jwtSignedKey"),
				UserIdentify: c.String("userIdentify"),
			}
			opt.MgoDB.URL = c.String("mgourl")
			opt.MgoDB.DbName = c.String("mgodbname")
			go regSigin()
			return server.StartServer(opt)

		},
	}

	err := app.Run(args)
	if err != nil {
		log.Println("启动失败:", err)
	}
}
func regSigin() {

	sigc := make(chan os.Signal, 1)
	signal.Notify(sigc, os.Interrupt, os.Kill)
	<-sigc
	go func() {
		signal.Notify(sigc, os.Interrupt, os.Kill)
		<-sigc
		log.Println("在此收到了退出程序信号，强制退出")
		os.Exit(9)
	}()
	log.Println("收到了退出程序信号")
	server.StopServer()
	os.Exit(0)

}
